/*
 * ValidationItem-Basic.js
 *
 * Created on 01-Dec-2005
 * 
 * Copyright (C) 2005-2006 Allan Lykke Christensen
 *                         allan.lykke.christensen@gmail.com
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

/**
 * Validation item for a form field that should not be empty upon validation.
 *
 * Usage:
 *   var item = new ValidationItemEmpty("fld", "valid", "invalid", 
 *                     "Field was not empty");
 *
 * @param field 
 *           ID of the field to be validated.
 * @param fieldValidClass
 *           Classes that the element should use when the input is valid
 * @param fieldInvalidClass
 *           Classes that the element should use when the input is invalid
 * @param errorMsg 
 *           Error message to display if the input does not validate
 */
function ValidationItemEmpty(field, fieldValidClass, fieldInvalidClass, errorMsg) {
    
    /** Field to validate. */
    this.field = field;
    
    /** Class to use if the form field is valid. */
    this.fieldValidClass = fieldValidClass;
    
    /** Class to use if the form field is invalid. */
    this.fieldInvalidClass = fieldInvalidClass;
    
    /** Error message to display if the input is invalid. */
    this.errorMsg = errorMsg;
    
    /**
     * Validates the form field.
     *
     * @return true if the form field is not empty.
     */
    function validate() {
        var element = document.getElementById(this.field);
        validated = element.value.match(/^\w+/);
        return validated;
    }
    
    this.validate = validate;
}

/**
 * A validation item for an e-mail address.
 *
 * Usage:
 *   var item = new ValidationItemEmailAddress("fld", "valid", "invalid", 
 *                     "Field was not an e-mail address");
 *
 * @param field 
 *           ID of the field to be validated.
 * @param fieldValidClass
 *           Classes that the element should use when the input is valid
 * @param fieldInvalidClass
 *           Classes that the element should use when the input is invalid
 * @param errorMsg 
 *           Error message to display if the input does not validate
 */
function ValidationItemEmailAddress(field, fieldValidClass, fieldInvalidClass, errorMsg) {

    /** Field to validate. */
    this.field = field;
    
    /** Class to use if the form field is valid. */
    this.fieldValidClass = fieldValidClass;
    
    /** Class to use if the form field is invalid. */
    this.fieldInvalidClass = fieldInvalidClass;
    
    /** Error message to display if the input is invalid. */
    this.errorMsg = errorMsg;

    /**
     * Validates the form field.
     *
     * @return true if the form field is a valid e-mail address.
     */	
	function validate() {
		var element = document.getElementById(this.field);
		validated = element.value.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*.\w+([-.]w+)*$/);
		return validated;
	}
	
	this.validate = validate;
}

/**
 * A validation item for a URL.
 *
 * @param field 
 *           ID of the field to be validated.
 * @param fieldValidClass
 *           Classes that the element should use when the input is valid
 * @param fieldInvalidClass
 *           Classes that the element should use when the input is invalid
 * @param errorMsg 
 *           Error message to display if the input does not validate
 */
function ValidationItemURL(field, fieldValidClass, fieldInvalidClass, errorMsg) {

    /** Field to validate. */
    this.field = field;
    
    /** Class to use if the form field is valid. */
    this.fieldValidClass = fieldValidClass;
    
    /** Class to use if the form field is invalid. */
    this.fieldInvalidClass = fieldInvalidClass;
    
    /** Error message to display if the input is invalid. */
    this.errorMsg = errorMsg;

    /**
     * Validates the form field.
     *
     * @return true if the form field is a valid HTTP/HTTPS URL.
     */		
	function validate() {
		var element = document.getElementById(this.field);
		validated = element.value.match(/^(http|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:\/~\+#]*[\w\-\@?^=%&\/~\+#])?/);
		return validated;
	}
	
	this.validate = validate;
}

/**
 * A validation item for a positive integer.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemPositiveInteger(field, fieldValidClass, fieldInvalidClass, errorMsg) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	
	function validate() {
		var element = document.getElementById(this.field);
		validated = element.value.match(/^\d+$/);
		return validated;
	}
	
	this.validate = validate;
}

/**
 * A validation item for an integer.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemInteger(field, fieldValidClass, fieldInvalidClass, errorMsg) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	
	function validate() {
		var element = document.getElementById(this.field);
		validated = element.value.match(/^[-+]*\d+$/);
		return validated;
	}
	
	this.validate = validate;
}

/**
 * A validation item for an IPv4 address.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemIPv4(field, fieldValidClass, fieldInvalidClass, errorMsg) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	
	function validate() {
		var element = document.getElementById(this.field);
		validated = element.value.match(/^\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b/);
		return validated;
	}
	
	this.validate = validate;
}

/**
 * A validation item for a number less than another number.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemLessThan(field, fieldValidClass, fieldInvalidClass, errorMsg, number) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.number = number;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		num1 = parseInt(this.number);
		num2 = parseInt(element.value);
		
		if (num1 < num2){
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}

/**
 * A validation item for a number greater than another number.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemGreaterThan(field, fieldValidClass, fieldInvalidClass, errorMsg, number) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.number = number;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		num1 = parseInt(this.number);
		num2 = parseInt(element.value);
		
		if (num1 > num2){
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}

/**
 * A validation item for a number between a lower and upperbound
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemBetween(field, fieldValidClass, fieldInvalidClass, errorMsg, lowerbound, upperbound) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.lowerbound = lowerbound;
	this.upperbound = upperbound;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		compare = parseInt(element.value);
		min = parseInt(lowerbound);
		max = parseInt(upperbound);
		
		if ((compare >= min) && (compare <= max)) {
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}

/**
 * A validation item for a validating that it has at least x amount of
 * characters.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemMinimumLength(field, fieldValidClass, fieldInvalidClass, errorMsg, minimum) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.minimum = minimum;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		if (element.value.length >= this.minimum){
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}

/**
 * A validation item for a validating that it has at most x amount of
 * characters.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemMaximumLength(field, fieldValidClass, fieldInvalidClass, errorMsg, maximum) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.maximum = maximum;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		if (element.value.length <= this.maximum){
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}

/**
 * A validation item for a validating that it has at least x amount of
 * characters and at most y amount of chracters.
 *
 * @param field ID of the field to be validated.
 * @param fieldValidClass Classes that the element should use when the input is valid
 * @param fieldInvalidClass Classes that the element should use when the input is invalid
 * @param errorMsg Error message to display if the input does not validate
 */
function ValidationItemBetweenLength(field, fieldValidClass, fieldInvalidClass, errorMsg, minimum, maximum) {
	this.field = field;
	this.fieldValidClass = fieldValidClass;
	this.fieldInvalidClass = fieldInvalidClass;
	this.errorMsg = errorMsg;
	this.maximum = maximum;
	this.minimum = minimum;
	
	function validate() {
		var element = document.getElementById(this.field);
		
		if ((element.value.length <= this.maximum) && (element.value.length >= this.minimum)){
			return true;
		}
		else {
			return false;
		}
	}
	
	this.validate = validate;
}
